﻿using System.Linq.Dynamic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ObjectBakery.Tests.TestDoubles;
using ObjectBakery.Tests.TestFactories;
using System.Linq;

namespace ObjectBakery.Tests
{
    
    
    /// <summary>
    ///This is a test class for DynamicLinqExtensionsTest and is intended
    ///to contain all DynamicLinqExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DynamicLinqExtensionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod]
        public void TestStringPredicateQueryWithStringValue()
        {
            var assets = new TestAssetFactory().Build();

            var subset = assets.Where("Name == \"Brochure\"").OrderBy("ID");
            //subset.Where(asset => asset.Name == "Brochure").OrderBy(asset => asset.ID);

            foreach (var asset in subset)
            {
                Assert.IsTrue(asset.Name == "Brochure");
            }
        }

        [TestMethod]
        public void TestStringPredicateQueryWithEnumValue()
        {
            var assets = new TestAssetFactory().Build();

            //Format is an enum "Jpg" is the enum value
            var subset = assets.Where("Format == \"Jpg\"").OrderBy("ID");

            foreach (var asset in subset)
            {
                Assert.IsTrue(asset.Format == AssetFormat.Jpg);
            }
        }

        [TestMethod]
        public void TestStringPredicateQueryWithCompoundExpression()
        {
            var assets = new TestAssetFactory().Build();

            //Format is an enum "Jpg" is the enum value
            var subset = assets.Where("Format == \"Jpg\" && ID > 1").OrderBy("ID");

            Assert.IsTrue(subset.ToList().Count > 1);
            foreach (var asset in subset)
            {
                Assert.IsTrue(asset.Format == AssetFormat.Jpg);
            }
        }

        [TestMethod]
        public void TestStringPredicateQueryWithContains()
        {
            var assets = new TestAssetFactory().Build();

            //Format is an enum "Jpg" is the enum value
            var subset = assets.Where("Name.Contains(\"Image\")").OrderBy("ID");

            Assert.IsTrue(subset.ToList().Count > 1);
            foreach (var asset in subset)
            {
                Assert.IsTrue(asset.Format == AssetFormat.Jpg);
            }
        }

        [TestMethod]
        public void TestStringPredicateQueryWithIntegerValue()
        {
            var products = new TestProductFactory().Build();

            var subset = products.Where("ID > 1").OrderBy("Name");

            foreach (var product in subset)
            {
                Assert.IsTrue(product.ID > 1);
            }
        }

        
    }
}
